package com.nowcoder.community.dao;

import com.nowcoder.community.entity.LoginTicket;
import org.apache.ibatis.annotations.*;

//这里的SQL语句没有写mapper对应的xml文件，而是采用了注解的方式
//Mapper写完最好都加个测试
//Deprecated： 不推荐使用
@Mapper
@Deprecated
public interface LoginTicketMapper {
    //插入数据
    @Insert({
            "insert into login_ticket (user_id, ticket, status, expired) ", // 后面加个空格，连在一起会出错
            "values (#{userId},#{ticket},#{status},#{expired})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id") //将id属性设为自动生成主键
    int insertLoginTicket(LoginTicket loginTicket);

    //根据 ticket 字段查询
    @Select({
            "select id,user_id,ticket,status,expired ",
            "from login_ticket where ticket=#{ticket}"
    })
    LoginTicket selectByTicket(String ticket);

    //根据 ticket 更新状态
    //这里是动态SQL的写法
    //@Update({
    //        "update login_ticket set status=#{status} where ticket=#{ticket} "
    //})
    @Update({
            "<script>", // 表示脚本
            "update login_ticket set status=#{status} where ticket=#{ticket} ",
            "<if test=\"ticket!=null\">",
            "and 1=1 ",
            "</if>",
            "</script>"
    })
    int updateStatus(String ticket, int status);
}
